package com.java.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.RaceComment;
import com.java.entity.RacePost;
import com.java.entity.event.RaceCommentEvent;
import com.java.entity.event.RacePostEvent;
import com.java.mapper.RaceAccountMapper;
import com.java.mapper.RaceCommentMapper;
import com.java.mapper.RacePostMapper;
import com.java.service.RacePostService;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class RacePostServiceImpl implements RacePostService {

	@Autowired
	private RacePostMapper postMapper;
	@Autowired
	private RaceCommentMapper commentMapper;
	@Autowired
	private RaceAccountMapper accountMapper;

	@Override
	public BaseRespone createPost(String userId, String text) {
		RacePost info = new RacePost().setPostId(MyTools.getUUID()).setPostUserId(userId).setPostText(text)
				.setPostTime(new Date());
		int row = postMapper.insertSelective(info);
		return BaseRespone.result(row, "已发布");
	}

	@Override
	public BaseRespone upodatePost(String postId, String text) {
		int row = postMapper.updateSelective(new RacePost().setPostId(postId).setPostText(text));
		return BaseRespone.result(row, "已修改");
	}

	@Override
	public BaseRespone delPost(String postId) {
		int row = postMapper.deleteByKey(postId);
		return BaseRespone.result(row, "已删除");
	}

	@Override
	public TableRespone getPosts(String userId, int page, int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (null != userId) {
			params.put("postUserId", userId);
		}
		List<RacePostEvent> list = postMapper.selectAll(params, page, size);
		for (RacePostEvent post : list) {
			post.setPostUser(accountMapper.selectByKey(post.getPostUserId()));
			Map<String, Object> cparams = new HashMap<String, Object>();
			cparams.put("commentPostId", post.getPostId());
			List<RaceCommentEvent> clist = commentMapper.selectAll(cparams, page, size);
			for (RaceCommentEvent comment : clist) {
				comment.setPost(postMapper.selectByKey(comment.getCommentPostId()));
				comment.setUser(accountMapper.selectByKey(comment.getCommentUserId()));
				comment.setCommentParent(commentMapper.selectByKey(comment.getCommentParentId()));
			}
			post.setComments(clist);
		}
		long count = postMapper.selectAllCount(params);
		return new TableRespone(list, count);
	}

	@Override
	public BaseRespone addComment(String userId, String postId, String parentId, String text) {
		RaceComment info = new RaceComment().setCommentId(MyTools.getUUID()).setCommentParentId(parentId)
				.setCommentPostId(postId).setCommentUserId(userId).setCommentText(text).setCommentTime(new Date());
		int row = commentMapper.insertSelective(info);
		return BaseRespone.result(row, "已评论");
	}

	@Override
	public BaseRespone delComment(String commentId) {
		int row = commentMapper.deleteByKey(commentId);
		return BaseRespone.result(row, "已删除");
	}

	@Override
	public TableRespone getComments(String userId, String postId, int page, int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (null != userId) {
			params.put("commentUserId", userId);
		}
		if (null != postId) {
			params.put("commentPostId", postId);
		}
		List<RaceCommentEvent> list = commentMapper.selectAll(params, page, size);
		for (RaceCommentEvent comment : list) {
			comment.setPost(postMapper.selectByKey(comment.getCommentPostId()));
			comment.setUser(accountMapper.selectByKey(comment.getCommentUserId()));
			comment.setCommentParent(commentMapper.selectByKey(comment.getCommentParentId()));
		}
		long count = commentMapper.selectAllCount(params);
		return new TableRespone(list, count);
	}
}
